#!/bin/bash
#####################################################
# 重新链接仓库， 并强制推送
# 注: 因为此项目仅有1人更新， 所以可以使用强制推送和拉取， 并不适用于多人合作的项目
#####################################################

################### Custom parameters start ###################
URL="https://gitee.com/wasZup/c-socket-test.git"

################### Custom parameters End ###################


_VERSION=1
MSG=""



# 重新链接仓库
re_link(){
    rm  -rf ./.git                   # 删除旧的.git
    git init                        # 初始化
    
    git remote add origin ${URL}
    # git push --set-upstream origin master
    
}



# 强制上传
force_upload(){
    
    git add    -A                       # 添加

    
    git commit -m "forceup_$1"          # 提交
    git push   --force origin master    # 强制推送到云端
}
#强制下载
force_download(){
    git fetch --all
    git reset --hard origin/master
}


refresh_gitignore(){
    git rm -r --cached .
    git add .
    git commit -m 'update .gitignore'
    git push origin master
}



git_add_tag()
{    
    tag_name=$1
    if [ $# = 0 ];then
        # 提示用户输入 tag 名称
        read -p  "Please enter tag name: "   tag_name

    fi    
    
    # 上传之前先推送到最新
    read -p  "commit msg: "   MSG
    force_upload $MSG
    
    
    # 判断 tag 名称是否为空
    if [[ -z "$tag_name" ]]; then
        echo "Error: tag name cannot be empty."
        exit 1
    fi
    
    # 执行 Git 添加 tag 命令
    git tag -a "$tag_name" -m "Added tag $tag_name $MSG"
    
    # 推送 tag 到远程仓库
    git push origin "$tag_name"
    
    # 输出完成信息
    echo "Tag $tag_name has been added successfully."

    # ./build.sh -bk "$MSG"
    
}

usage(){
    echo "################################### MEUN ###################################"
    echo "$0                MEUN [u|d|i]"
    echo "$0 -u             force upload        (push)将本地强制覆盖到云端"
    echo "$0 -d             force download      (pull)将云端强制覆盖到本地"
    echo "$0 -l             re-link             (remote)重新链接云端仓库"
    echo "$0 -i             refresh .gitignore  刷新'.gitignore'文件"
    echo "$0 -t  [msg]      git tag             添加tag并提交tag"
    echo ""
    
    echo "
                            git tag -a {标签名} -m \"{标签信息}\" {提交ID}      EG:git tag -a v1.0.0 -m \"Release version 1.0.0\" HEAD
                            git tag -d v1.0.0                   删除本地标签
                            git push origin : refs/tags/v1.0.0   删除云端的 v1.0.0标签
    git push origin --tags              推送本地所有标签"
    
    echo "############################################################################"
}



main(){
    case ${1} in
        -l|l|link)
            re_link
        ;;
        
        -u|u|push)
            force_upload
        ;;
        
        -d|d|pull)
            force_download
        ;;
        
        -i|i)
            refresh_gitignore
        ;;
        
        -t|t|tag)
            git_add_tag ${2}
        ;;
        
        -a|a|A|-A)
            echo "option='${1}'"
        ;;
        
        *)
            usage
        ;;
        
    esac
}

main $1 $2

